package com.example;

import com.config.World;
import com.example.annotation.EnableHello;
import com.example.annotation.EnableHelloSelector;
import com.example.common.event.MessEvent;
import com.example.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@EnableAsync
@Slf4j
//@EnableHello
//@EnableHelloSelector(isLinux = false)
@RestController
@SpringBootApplication
public class SpringbootStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootStudyApplication.class, args);
    }

    @Autowired
    ApplicationContext applicationContext;

    @GetMapping("/test")
    public Object test() {

        MessEvent event = new MessEvent(this, 0);
        event.setPostId(1L);
        event.setFromUserId(99L);

        applicationContext.publishEvent(event);

        log.info("------------------>test");

        return "hello world";
    }

    @PostMapping("/test2")
    public Object test2(@Valid User user, BindingResult bindingResult) {

        boolean hasErrors = bindingResult.hasErrors();
        List<ObjectError> allErrors = bindingResult.getAllErrors();

        System.out.println(allErrors);

        return user.toString();
    }

}
